//
//  CCColor.swift
//  SwiftZCC
//
//  Created by Mr.Hong on 2019/12/21.
//  Copyright © 2019 Mr.Hong. All rights reserved.
//

import UIKit

extension UIColor {
    // 便利构造函数
    public convenience init(cnm_hexString: String) {
        var cStr = cnm_hexString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased() as NSString;
        
        if(cStr.hasPrefix("0x")) {
            cStr = cStr.substring(from: 2) as NSString
        }
        
        if(cStr.hasPrefix("#")){
            cStr = cStr.substring(from: 1) as NSString
        }
                
        let rStr = (cStr as NSString).substring(to: 2)
        let gStr = ((cStr as NSString).substring(from: 2) as NSString).substring(to: 2)
        let bStr = ((cStr as NSString).substring(from: 4) as NSString).substring(to: 2)
        
        var r : UInt32 = 0x0
        var g : UInt32 = 0x0
        var b : UInt32 = 0x0
        
        Scanner.init(string: rStr).scanHexInt32(&r);
        Scanner.init(string: gStr).scanHexInt32(&g);
        Scanner.init(string: bStr).scanHexInt32(&b);
        
        let red   = CGFloat(r) / 255.0
        let green = CGFloat(g) / 255.0
        let blue  = CGFloat(b) / 255.0
        
        self.init(red: red, green: green, blue: blue, alpha: 1)
    }
    
    public convenience init(_ red: Int, _ green: Int, _ blue: Int, alpha: CGFloat = 1.0) {
        self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: alpha)
    }
    
    // 随机色
    public static var cnm_randomColor: UIColor {
        get {
            let red = CGFloat(arc4random()%256)/255.0
            let green = CGFloat(arc4random()%256)/255.0
            let blue = CGFloat(arc4random()%256)/255.0
            return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
        }
    }
}

extension UIColor {
    /// 主题色
    public static let cnm_FFDA2A = UIColor(cnm_hexString: "FFDA2A")
    /// 背景颜色
    public static let cnm_F4F4F4 = UIColor(cnm_hexString: "F4F4F4")
    /// 淡黑色
    public static let cnm_666666 = UIColor(cnm_hexString: "666666")
    /// 分割线颜色
    public static let cnm_C6C6C6 = UIColor(cnm_hexString: "C6C6C6")
    /// 暖灰颜色
    public static let cnm_FAF9F5 = UIColor(cnm_hexString: "FAF9F5")
}
